package controller;

import java.io.IOException;
import java.util.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.Advertisement;
import bean.CartItem;

import connection.ServiceProxy;

/**
 * Servlet implementation class Confirm
 */
public class Confirm extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Confirm() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		if(request.getParameter("submit").equals("Confirm")) {
			System.out.println("harryIn");
			Double ccn;
			String result = null, destination=null;
			String message = null;
			boolean error = false;
			HttpSession session = request.getSession();
			String userId = (String)session.getAttribute("userId");
			ServiceProxy oProxy = new ServiceProxy();
			
			String name = request.getParameter("name");
			String CCN = request.getParameter("ccn");
			String cvv = request.getParameter("cvv");
			String expiryMonth = request.getParameter("month");
			String expiryYear = request.getParameter("year");
			
			result = oProxy.checkAvailableQuantityForItemsInCart(userId);
			System.out.println(CCN);
			if(result.equals("true")){
				
				if(name.equals("")) {
					message = "Please provide name on credit card";
					error = true;
				}
				else if(CCN.equals("")){
					message = "Please provide Credit Card Number";
					error = true;
				}
				
				if(error == false) {
					try {
						ccn = Double.parseDouble(CCN);
					}
					catch(NumberFormatException e) {
						error = true;
						message = "Invalid Credit Card Number";
					}					
				}
				
				if(error == false) {
					if(CCN.length() > 16) {
						error = true;
						message = "Credit Card Number cannot be greater than 16 digits";
						
					}
					else if(CCN.length() < 16){
						error = true;
						message = "Credit Card Number cannot be less than 16 digits";
						
					}
				}
				
				if(error == false) {
					try {
						Integer.parseInt(cvv);
						
						if(cvv.length() > 3) {
							error = true;
							message = "cvv code cannot be greater than 3 digits";
							
						}
						else if(cvv.length() < 3){
							error = true;
							message = "cvv code cannot be less than 3 digits";
							
						}
						
						if(error == false) {
							Calendar cal = Calendar.getInstance();
							if(Integer.parseInt(expiryYear) == cal.get(Calendar.YEAR)) {
								if(Integer.parseInt(expiryMonth) <  cal.get(Calendar.MONTH)) {
									error = true;
									message = "Invalid Expiry Date";
								}
							}
						}
						
						
					}catch(NumberFormatException e){
						error = true;
						message = "Invalid ccv code";
						
					}
					
				}
				
				if(error == false) {
					
					System.out.println("harryIn");
					result = oProxy.purchaseItemsInCart(userId);
					
					if(result.equals("true")) {
						destination = "/ViewPurchasedItem";
						message = "Order Commited Successfully";
					}
					else {
						destination = "/home";
						message = "Please try again later";
					}
				}
				else
					destination = "/Checkout";
				
								
			}
			else if(result.equals("false")) {
				message = "Available Quantity is less than Ordered Quantity. Kindly update the cart";
				destination = "/ViewCart";
			}
			
			
			request.setAttribute("message", message);
			RequestDispatcher rd = this.getServletContext().getRequestDispatcher(destination);
			rd.forward(request, response);
		}
	}

}
